package library.controller;

import library.ErrorCode;
import library.ErrorMessage;
import library.entity.Librarian;

import java.sql.SQLException;

public class LibrarianController extends AbstractController<Librarian> {
	
	public LibrarianController() throws SQLException{
		super(Librarian.class);
	}
	
	public Librarian check(String name, String password){
		Librarian librarian = this.getLibrarian(name);

        if(librarian != null && librarian.checkPassword(password))
		    return librarian;
        else
            return null;
	}
	
	public Librarian getLibrarian(String userName){
        try {
            return selectOnce("userName", userName);
        } catch (Exception e) {
            return null;
        }
    }

    public boolean create(Librarian newLibrarian){
        if(getLibrarian(newLibrarian.getUserName()) != null){
            lastError = new ErrorMessage(ErrorCode.DUPLICATE_USER_FOUND);
            return false;
        }
        return super.insert(newLibrarian);
    }

/*    public boolean delete(Librarian deleteLibrarian) {
        try {
            return super.delete(deleteLibrarian);
        } catch (SQLException e) {
            lastError = new ErrorMessage(ErrorCode.UNEXPECTED_ERROR, e);
            return false;
        }
    }*/

    public boolean delete(String deleteLibrarianName) {
        Librarian delLibrarian = this.getLibrarian(deleteLibrarianName);
        if(delLibrarian == null){
            lastError = new ErrorMessage(ErrorCode.LIBRARIAN_NOT_FOUND);
            return false;
        }
        return super.delete(delLibrarian);
    }
    
    public boolean update(Librarian librarian){
    	return super.update(librarian);
    }
}
